<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="vue.js"></script>
    <link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
    <style>
        .title{            
            background-color: #E6E6E6;
            border: 1px solid #A5A5A5;
            display: flex;
            justify-content: space-between;
            border-radius: 3px;
            height: 35px;
            line-height: 35px;
            font-size: 16px;
        }
     
        section>div:nth-child(2) {
            height: 0;
            transition:all 1s;
            overflow: hidden;
        }

    </style>
    <title>Document</title>
</head>

<body>
    <main id="app">
        <section :id="item.title" v-showa='{name:item.title,length:item.content.length}' v-for="(item, index) in allMessage" :key="index">
            <div class="title" @click='show(item.title)'>
                <div>
                    <span class="fa fa-chevron-right"></span>
                    <span>{{item.title}}</span>
                </div>
                <span>
                    1/{{item.content.length}}
                </span>
            </div>
            <transition>
                <div :class='item.title'>   
                    <content-com :content='content' v-for="(content, i) in item.content" :key="i"></content-com>
                </div>
            </transition>
        </section>
    </main>
    <script id="content" type="text/html">
        <div>
                <h1>{{content.name}}</h1>
                <h3>{{content.message}}</h3>
                <hr>
        </div>
    </script>
    <script>
        Vue.component('content-com', {
            template: '#content',
            props: ['content']
        })
        
        Vue.directive('showa',{
            inserted(el,args){
                var str = '.'+args.value.name+' .'+args.value.name+'{'+ 'height:' + args.value.length*118 +'px;}'                
                var heightStyle = document.createElement('style')
                heightStyle.innerHTML = str
                document.head.appendChild(heightStyle)                                
            }
        })

        var vue = new Vue({
            el: '#app',
            data: {
                allMessage: [
                    {
                        title: '同学',
                        content: [
                            {
                                name: '小明',
                                message: '世界那么大'
                            },
                            {
                                name: '小美',
                                message: 'happy new year'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            }
                        ]
                    },
                    {
                        title: '家人',
                        content: [
                            {
                                name: '小明',
                                message: '世界那么大'
                            },
                            {
                                name: '小美',
                                message: 'happy new year'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            }
                        ]
                    },
                    {
                        title: '朋友',
                        content: [
                            {
                                name: '小明',
                                message: '世界那么大'
                            },
                            {
                                name: '小美',
                                message: 'happy new year'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            },
                            {
                                name: '小王',
                                message: '请叫我大王'
                            }
                        ]
                    }
                ],               
            },
            methods: {
                show(e) {
                    document.querySelector('main').classList.toggle(e)
                    document.getElementById(e).firstChild.firstChild.firstChild.classList.toggle('fa-chevron-down')
                }
            },
        })
    </script>
</body>

</html>